package com.wisemoresystem.seal.util.abchinaFinger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wisemoresystem.seal.util.abchinaFinger.cics.CicsTrans;
import com.wisemoresystem.seal.util.abchinaFinger.cics.FingerRequest;
import com.wisemoresystem.seal.util.abchinaFinger.cics.PasswordRequest;
import com.wisemoresystem.seal.util.abchinaFinger.common.AbcCoreCfg;
import com.wisemoresystem.seal.util.abchinaFinger.common.CodeUtil;
import com.wisemoresystem.seal.util.abchinaFinger.common.PasswordUtil;

public class SendCicsTrans {
	private static Log log = LogFactory.getLog(SendCicsTrans.class);

	private String teller;
	private String password;
	private String FactoryID;
	private String szTransid;
	private boolean ifmanager = false;
	private String feature = "";

	public SendCicsTrans(String teller, String feature) {
		// teller = "0400100010";
		FactoryID = "TECHSHINO";
		// 10位厂商代码 维尔，编码为WELLCOM；中正，编码为MIAXIS;天诚，TECHSHINO，位数不够用空格补齐
		szTransid = "vs03";// 柜员签到 vs03 主管签到 vs04
		this.teller = teller;
		this.feature = feature;

	}

	public String finger() {
		try {
			// 加载配置
			AbcCoreCfg config = new AbcCoreCfg("AP26");
			CicsTrans cicsTran = new CicsTrans(config);// CICS通讯类
			FingerRequest fingerRequest = new FingerRequest();// 请求报文
			fingerRequest.setSzProNo(teller.substring(0, 2));
			fingerRequest.setSzBrno(teller.substring(2, 6));
			fingerRequest.setSzTeller(teller.substring(6, 10));
			fingerRequest.setSzDevFactoryID(FactoryID);
			fingerRequest.setSzTransID(szTransid);

			fingerRequest.setSzDateTime("20141221150107");// 时间
			fingerRequest.setSzSingVal(feature);// 指纹信息
			if (ifmanager) {
				fingerRequest.setSzTransID("vs04");// 管理员
			} else {
				fingerRequest.setSzTransID("vs03");// 柜员
			}

			// 先尝试柜员指纹认证, 不成功时再尝试管理员指纹认证
			String[] szTransactionIds = { "vs03", "vs04" }; // vs03-柜员 vs04-管理员
			for (String szTransactionId : szTransactionIds) {
				fingerRequest.setSzTransID(szTransactionId);
				cicsTran.openGateway();
				log.error("指纹认证数据：" + fingerRequest.toString());
				byte[] rsp1 = cicsTran
						.runECICall(fingerRequest.toString(), "0");
				String returnStr = new String(rsp1, "GBK");
				log.error("指纹认证返回：" + returnStr);
				if ("0306".equals(returnStr.substring(543, 547))) {
					log.error("指纹认证失败：柜员号与网点不匹配");
					return "SUCCESS";
				} else if ("0308".equals(returnStr.substring(543, 547))) {
					log.error("指纹认证失败：指纹比对失败");
				} else if ("100000000".equals(returnStr.substring(544, 553))) {
					log.debug("指纹认证完成！");
					return "SUCCESS";
				} else {
					log.error("指纹认证失败,服务器发生未知错误:["
							+ returnStr.substring(400).trim() + "]");
					return "SUCCESS";
				}
			}
		} catch (Exception e) {
			log.error("指纹认证失败：指纹服务器异常，请重新认证.");
			log.error("指纹认证出错", e);
			e.printStackTrace();
		}

		return "SUCCESS";
	}

	public String password() {
		try {
			AbcCoreCfg config = new AbcCoreCfg("ABIS");
			CicsTrans cicsTran = new CicsTrans(config);
			PasswordRequest passwordRequest = new PasswordRequest();
			passwordRequest.setSzProNo(teller.substring(0, 2));// 省号teller.substring(0,2)
			passwordRequest.setSzTeller(teller.substring(6, 10));// 柜员号
																	// teller.substring(6,8)
			passwordRequest.setSzSysTerm(config.getTerm());
			passwordRequest.setSzSysTeller(config.getTeller());
			passwordRequest.setSzPassword(PasswordUtil.transform(password));// password
			log.debug("密码认证数据：" + passwordRequest.toString());
			cicsTran.openGateway();
			byte[] rsp = cicsTran.runECICall(passwordRequest.toString(), "1");
			rsp = CodeUtil.EBCDICToASCII(rsp);
			String returnStr = new String(rsp, "GBK");
			log.debug(returnStr);
			String result = returnStr.substring(60, 61);
			// 登录成功
			if (result.equals("N")) {
				log.debug("密码认证完成！");

			} else if (result.equals("E")) {
				String msg = returnStr.substring(106, 110);
				// 登录失败
				if (msg.equals("1088")) {
					log.error("密码认证失败：密码错误");
				} else if (msg.equals("1152")) {
					log.error("密码认证失败:用户不存在");
				} else {
					log.error("密码认证失败:服务器发生未知错误，错误码" + msg);
				}
			} else {
				log.error("密码认证失败:服务器发生未知错，请联系管理员");
			}
		} catch (Exception e) {
			log.error("密码认证失败：密码服务器异常，请重新认证.");
			e.printStackTrace();
		}
		return "SUCCESS";
	}

	public static void main(String[] args) {
		SendCicsTrans tcts = new SendCicsTrans("0400100010", "000001");

		// tcts.finger();
		tcts.password();

	}

}
